import { _decorator, animation, Animation, AnimationClip, Component, Node, Sprite, SpriteFrame, UITransform } from 'cc';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, SHAKE_TYPE_ENUM } from '../../Enums';
import { EventManager } from '../../Runtime/EventManager';
import { DataManager } from '../../Runtime/DataManager';
import { IEntity } from '../../Levels';
import { EntityManager } from '../../Base/EntityManager';
import { BurstStateMachine } from './BurstStateMachine';
import { TILE_HEIGHT, TILE_WIDTH } from '../TIle/TileManager';
const { ccclass, property } = _decorator;


@ccclass('BurstManager')
export class BurstManager extends EntityManager {

    async init(params: IEntity) {
        this.fsm = this.addComponent(BurstStateMachine);
        await this.fsm.init();
        super.init(params);

        const transform = this.node.getComponent(UITransform);
        transform.setContentSize(TILE_WIDTH, TILE_HEIGHT);
        super.update();
        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onBurst, this);
    }

    onDestroy(): void {
        super.onDestroy();
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onBurst);

    }

    update(): void {

        this.node.setPosition(this.x * TILE_WIDTH, -this.y * TILE_HEIGHT);
    }


    onBurst() {
        if (this.state === ENTITY_STATE_ENUM.DEATH || !DataManager.Instance.player) {
            return;
        }
        const { x: playerX, y: PlayerY } = DataManager.Instance.player;

        if (this.x === playerX && this.y === PlayerY && this.state === ENTITY_STATE_ENUM.IDLE) {
            this.state = ENTITY_STATE_ENUM.ATTACK;

        } else if (this.state === ENTITY_STATE_ENUM.ATTACK) {
            this.state = ENTITY_STATE_ENUM.DEATH;
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAK, SHAKE_TYPE_ENUM.BOTTOM);
            if (this.x === playerX && this.y === PlayerY) {
                EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER, ENTITY_STATE_ENUM.AIRDEATH);

            }

        }
    }

}


